#include "stdafx.h"
#include "DateTime.h"

DateTime::DateTime()
:m_uiYear(0)
,m_uiMonth(0)
,m_uiDay(0)
,m_uiHour(0)
,m_uiMinute(0)
,m_uiSecond(0)
{
	memset(m_szBuffer,'0',DATETIME_LENGTH);
}

DateTime::DateTime(char* p_szDateTime, unsigned int p_uiLength)
	:m_uiYear(0)
	,m_uiMonth(0)
	,m_uiDay(0)
	,m_uiHour(0)
	,m_uiMinute(0)
	,m_uiSecond(0)
{
	memset(m_szBuffer,'0',DATETIME_LENGTH);
	if (NULL == p_szDateTime)
	{
		LOG(ERROR) << "DateTime class construct error : NULL pointer";
		return;
	}
	if (0 == p_uiLength)
	{
		LOG(ERROR) << "DateTime class construct error : Length equals to 0";
		return;
	}
	if (p_uiLength > DATETIME_LENGTH)
	{
		LOG(ERROR) << "DateTime class construct error : Length greater than DATETIME_LENGTH";
		return;
	}

	// Format buffer associate with parameter
	memcpy(m_szBuffer,p_szDateTime,p_uiLength);
	
	// Set value
	SetValue(m_szBuffer,m_uiYear,4);		// Year
	SetValue(m_szBuffer+4,m_uiMonth,2);		// Month
	SetValue(m_szBuffer+6,m_uiDay,2);		// Day
	SetValue(m_szBuffer+8,m_uiHour,2);		// Hour
	SetValue(m_szBuffer+10,m_uiMinute,2);	// Minute
	SetValue(m_szBuffer+12,m_uiSecond,2);	// Second

	// Verify value
	if(m_uiMonth > 12 || m_uiDay > 31 || m_uiHour > 24 || m_uiMinute > 60 || m_uiSecond > 60)
	{
		LOG(ERROR) << "DateTime class format error";
		m_uiYear = m_uiMonth = m_uiDay = m_uiHour = m_uiMinute = m_uiSecond = 0;
	}
}

DateTime::DateTime(string p_strDateTime)
:m_uiYear(0)
,m_uiMonth(0)
,m_uiDay(0)
,m_uiHour(0)
,m_uiMinute(0)
,m_uiSecond(0)
{
	memset(m_szBuffer,'0',DATETIME_LENGTH);
	if (0 == p_strDateTime.length())
	{
		LOG(ERROR) << "DateTime class construct error : Length equals to 0";
		return;
	}
	if (p_strDateTime.length() > DATETIME_LENGTH)
	{
		LOG(ERROR) << "DateTime class construct error : Length greater than DATETIME_LENGTH";
		return;
	}

	// Format buffer associate with parameter
	memcpy(m_szBuffer,p_strDateTime.data(),p_strDateTime.length());

	// Set value
	SetValue(m_szBuffer,m_uiYear,4);		// Year
	SetValue(m_szBuffer+4,m_uiMonth,2);		// Month
	SetValue(m_szBuffer+6,m_uiDay,2);		// Day
	SetValue(m_szBuffer+8,m_uiHour,2);		// Hour
	SetValue(m_szBuffer+10,m_uiMinute,2);	// Minute
	SetValue(m_szBuffer+12,m_uiSecond,2);	// Second

	// Verify value
	if(m_uiMonth > 12 || m_uiDay > 31 || m_uiHour > 24 || m_uiMinute > 60 || m_uiSecond > 60)
	{
		LOG(ERROR) << "DateTime class format error";
		m_uiYear = m_uiMonth = m_uiDay = m_uiHour = m_uiMinute = m_uiSecond = 0;
	}
}

string DateTime::GetLocalDate()
{
	time_t stNow;
	struct tm* pstLocalTime;
	char szDate[9] = {0};

	time(&stNow);
	pstLocalTime = localtime(&stNow);
	sprintf(szDate,"%d%02d%02d",pstLocalTime->tm_year + 1900,pstLocalTime->tm_mon + 1,pstLocalTime->tm_mday);
	return string(szDate);
}

string DateTime::GetLocalTime()
{
	time_t stNow;
	struct tm* pstLocalTime;
	char szTime[7] = {0};

	time(&stNow);
	pstLocalTime = localtime(&stNow);
	sprintf(szTime,"%02d%02d%02d",pstLocalTime->tm_hour,pstLocalTime->tm_min,pstLocalTime->tm_sec);
	return string(szTime);
}

string DateTime::GetLocalDateTime()
{
	time_t stNow;
	struct tm* pstLocalTime;
	char szDateTime[DATETIME_LENGTH+1] = {0};

	time(&stNow);
	pstLocalTime = localtime(&stNow);
	sprintf(szDateTime,
			"%d%02d%02d%02d%02d%02d",
			pstLocalTime->tm_year + 1900,
			pstLocalTime->tm_mon + 1,
			pstLocalTime->tm_mday,
			pstLocalTime->tm_hour,
			pstLocalTime->tm_min,
			pstLocalTime->tm_sec
			);

	return string(szDateTime);
}

bool DateTime::IsWeekend()
{
	time_t stNow;
	struct tm* pstLocalTime;
	time(&stNow);
	pstLocalTime = localtime(&stNow);
	if (pstLocalTime->tm_wday == 6 || pstLocalTime->tm_wday == 7)
	{
		return true;
	}
	else
	{
		return false;
	}
}

string DateTime::DateToString()
{
	char szDate[9] = {0};
	sprintf(szDate,"%d%02d%02d",m_uiYear,m_uiMonth,m_uiDay);
	return string(szDate);
}

string DateTime::TimeToString()
{
	char szTime[7] = {0};
	sprintf(szTime,"%02d%02d%02d",m_uiHour,m_uiMinute,m_uiSecond);
	return string(szTime);
}

string DateTime::DateTimeToString()
{
	char szDateTime[DATETIME_LENGTH+1] = {0};
	sprintf(szDateTime,"%d%02d%02d%02d%02d%02d",m_uiYear,m_uiMonth,m_uiDay,m_uiHour,m_uiMinute,m_uiSecond);
	return string(szDateTime);
}

void DateTime::SetToCurrentDateTime()
{
	time_t stNow;
	struct tm* pstLocalTime;
	time(&stNow);
	pstLocalTime = localtime(&stNow);
	m_uiYear = pstLocalTime->tm_year + 1900;
	m_uiMonth = pstLocalTime->tm_mon + 1;
	m_uiDay = pstLocalTime->tm_mday;
	m_uiHour = pstLocalTime->tm_hour;
	m_uiMinute = pstLocalTime->tm_min;
	m_uiSecond = pstLocalTime->tm_sec;
}

void DateTime:: SetToCurrentDate()
{
	time_t stNow;
	struct tm* pstLocalTime;
	time(&stNow);
	pstLocalTime = localtime(&stNow);
	m_uiYear = pstLocalTime->tm_year + 1900;
	m_uiMonth = pstLocalTime->tm_mon + 1;
	m_uiDay = pstLocalTime->tm_mday;
	m_uiHour = 0;
	m_uiMinute = 0;
	m_uiSecond = 0;
}

void DateTime::SetToCurrentTime()
{
	time_t stNow;
	struct tm* pstLocalTime;
	time(&stNow);
	pstLocalTime = localtime(&stNow);
	m_uiYear = 0;
	m_uiMonth = 0;
	m_uiDay = 0;
	m_uiHour = pstLocalTime->tm_hour;
	m_uiMinute = pstLocalTime->tm_min;
	m_uiSecond = pstLocalTime->tm_sec;
}

void DateTime::SetDateTime(string p_strDateTime)
{
	memset(m_szBuffer,'0',DATETIME_LENGTH);
	if (0 == p_strDateTime.length())
	{
		LOG(ERROR) << "DateTime class construct error : Length equals to 0";
		return;
	}
	if (p_strDateTime.length() > DATETIME_LENGTH)
	{
		LOG(ERROR) << "DateTime class construct error : Length greater than DATETIME_LENGTH";
		return;
	}

	// Format buffer associate with parameter
	memcpy(m_szBuffer,p_strDateTime.data(),p_strDateTime.length());

	// Set value
	SetValue(m_szBuffer,m_uiYear,4);		// Year
	SetValue(m_szBuffer+4,m_uiMonth,2);		// Month
	SetValue(m_szBuffer+6,m_uiDay,2);		// Day
	SetValue(m_szBuffer+8,m_uiHour,2);		// Hour
	SetValue(m_szBuffer+10,m_uiMinute,2);	// Minute
	SetValue(m_szBuffer+12,m_uiSecond,2);	// Second

	// Verify value
	if(m_uiMonth > 12 || m_uiDay > 31 || m_uiHour > 24 || m_uiMinute > 60 || m_uiSecond > 60)
	{
		LOG(ERROR) << "DateTime class format error";
		m_uiYear = m_uiMonth = m_uiDay = m_uiHour = m_uiMinute = m_uiSecond = 0;
	}
}

void DateTime::SetValue(char* p_szSourceBuffer,unsigned int& p_uiDestValue,unsigned int p_uiSize)
{
	char szTempt[DATETIME_LENGTH+1] = {0};
	memcpy(szTempt,p_szSourceBuffer,p_uiSize);
	p_uiDestValue = atoi(szTempt);
}

unsigned int DateTime::operator - (const DateTime& p_crRight)
{
	return (this->m_uiDay - p_crRight.m_uiDay)*3600*24 + (this->m_uiHour - p_crRight.m_uiHour)*3600 + (this->m_uiMinute - p_crRight.m_uiMinute)*60 + (this->m_uiSecond - p_crRight.m_uiSecond);
}

unsigned int DateTime::GetYear()
{
	return m_uiYear;
}

unsigned int DateTime::GetMonth()
{
	return m_uiMonth;
}

unsigned int DateTime::GetDay()
{
	return m_uiDay;
}